package com.yangjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yangjian.bean.ShoppingCart;
import com.yangjian.dao.ShoppingCartDao;
import com.yangjian.service.ShoppingCartService;
import com.yangjian.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao dao;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //构建条件对象
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //添加条件用户id
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //添加条件菜品id
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //添加条件套餐id
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = dao.selectOne(lqw);

        if (shoppingCart1 == null) {//判断购物车有没有数据存在,没有数据就执行往购物车表里添加数据
            //补充数据
            shoppingCart.setUserId(BaseContext.getCurrentId());//补充用户id
            shoppingCart.setCreateTime(LocalDateTime.now());//补充添加购物车时间
            shoppingCart.setNumber(1);//给购物车里的菜品设置一个初始数量
            //添加到购物车
            dao.insert(shoppingCart);
            return shoppingCart;
        }
        //如果不等于努力了，意味着购物车数据库里有数据，执行修改操作,修改菜品数量
        shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
        //更新操作
        dao.updateById(shoppingCart1);


        return shoppingCart1;
    }


    /**
     * 根据用户id查询购物车
     *
     * @param uid
     * @return
     */
    @Override
    public List<ShoppingCart> findByUserUd(Long uid) {
        //构建条件对象
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(ShoppingCart::getUserId, uid);
        //查询
        List<ShoppingCart> shoppingCarts = dao.selectList(lqw);
        return shoppingCarts;
    }

    /**
     * 修改购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public int update(ShoppingCart shoppingCart) {
        //构建条件对象
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //添加条件用户id
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //添加条件菜品id
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //添加条件套餐id
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = dao.selectOne(lqw);
        System.out.println("shoppingCart1 = " + shoppingCart1);
        Integer number = shoppingCart1.getNumber();

        if (number >1) {
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            int row = dao.updateById(shoppingCart1);
            return row;


        }else {

            int row = dao.deleteById(shoppingCart1);
            return row;
        }



    }

    /**
     * 清空购物车
     *
     * @param uid
     * @return
     */
    @Override
    public int delete(Long uid) {
        LambdaQueryWrapper<ShoppingCart> lqw  = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,uid);
        int row = dao.delete(lqw);
        return row;
    }


}
